package com.whlilong.jxc.print;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.printing.PrintDialog;
import org.eclipse.swt.printing.Printer;
import org.eclipse.swt.printing.PrinterData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.jface.viewers.TableViewer;
import org.eclipse.jface.viewers.TableViewerColumn;

class PrintSWTTableExample {
  private Display display;
private Table tablev;
protected PDocument document;

  public PrintSWTTableExample() {

    // create some gui with a SWT table
    display = new Display();
    final Shell shell = new Shell(display);
    final org.eclipse.swt.layout.GridLayout gridLayout = new org.eclipse.swt.layout.GridLayout();
    gridLayout.numColumns = 2;
    shell.setLayout(gridLayout);
    final Color red = display.getSystemColor(SWT.COLOR_RED);
    
    TableViewer tableViewer = new TableViewer(shell, SWT.FULL_SELECTION);
	tableViewer.setUseHashlookup(true);

	GridData gd = new GridData(GridData.FILL_BOTH);
	gd.horizontalSpan = 4;
	tablev = tableViewer.getTable();
	tablev.setLayoutData(gd);
	tablev.setToolTipText("点击单元格对数据进行修改！");
	// 设置行高
	tablev.addListener(SWT.MeasureItem, new Listener() {
		@Override
		public void handleEvent(Event event) {
			// TODO Auto-generated method stub
			event.height = 20;
		}
	});
	tablev.setHeaderVisible(true);
	tablev.setLinesVisible(true);
	
	TableColumn tableColumn = new TableColumn(tablev,SWT.NONE);
	tableColumn.setText("ID");
	tableColumn.setWidth(50);
	TableColumn tablColumn = new TableColumn(tablev,SWT.NONE);
	tablColumn.setText("name");
	tablColumn.setWidth(50);
	
	tableViewer.setContentProvider(new DistrictTableContentProvider());
	tableViewer.setLabelProvider(new DistrictTableLableProvider());
	
	tableViewer.setInput(Keshang.sample());

//    final Table table_swt = new Table(shell, SWT.BORDER
//        | SWT.FULL_SELECTION);
//    final GridData gridData = new GridData(GridData.FILL_BOTH);
//    gridData.horizontalSpan = 2;
//    table_swt.setLayoutData(gridData);
//    table_swt.setHeaderVisible(true);
//    table_swt.setLinesVisible(true);
//    TableColumn column1 = new TableColumn(table_swt, SWT.NONE);
//    TableColumn column2 = new TableColumn(table_swt, SWT.NONE);
//    TableColumn column3 = new TableColumn(table_swt, SWT.NONE);
//    column1.setText("Column1");
//    column2.setText("Column2");
//    column3.setText("Column3");
//
//    for (int i = 0; i < 100; i++) {
//      TableItem item = new TableItem(table_swt, SWT.NONE);
//      item.setText(new String[] { "cell " + i + " 123456789123456789123456789123456789123456789123456789", "cell " + i + "123456789123456789",
//          "cell " + i + "1" });
//    }
//    column1.pack();
//    column2.pack();
//    column3.pack();

    final Button butPrint = new Button(shell, SWT.NONE);
    butPrint.addSelectionListener(new SelectionAdapter() {

	public void widgetSelected(SelectionEvent e) {
        // create a document with default settings from PageSetup
        final PDocument doc = new PDocument(
            "SWT Table Printing Example");

        // put some header text on it
        PTextBox t;

        t = new PTextBox(doc);
        
        t.setText("武汉立隆");

        new PVSpace(doc, 0.5);
        new PHLine(doc, 0.02, SWT.COLOR_RED);
        new PVSpace(doc, 0.5);

        // create the table
        SWTPTable table = new SWTPTable(doc);
        table.setTable(tablev);
        table.setBoxProvider(new PTableBoxProvider());
        PrintPreview pr = new PrintPreview(null, "Test", IconSource
            .getImage("print"), doc);
        pr.open();
        
      }
    });
    butPrint.setText("Print Preview");

    final Button butClose = new Button(shell, SWT.NONE);
    butClose.addSelectionListener(new SelectionAdapter() {
      public void widgetSelected(SelectionEvent e) {
        shell.dispose();
      }
    });
    butClose.setLayoutData(new GridData(GridData.HORIZONTAL_ALIGN_END));
    butClose.setText("Close");

    // Show the shell
    shell.setSize(300, 400);
    shell.setText("SWT Table Printing Example");
    shell.setVisible(true);

    while (!shell.isDisposed()) {
      if (!display.readAndDispatch())
        display.sleep();
    }

    display.dispose();
  }

  /**
   * This function would print the document witout the print preview.
   * 
   * @param doc
   */
  public void print(PDocument doc) {
    PrintDialog dialog = new PrintDialog(null, SWT.BORDER);
    PrinterData data = dialog.open();
    if (data == null)
      return;
    if (data.printToFile) {
      data.fileName = "print.out"; // you probably want to ask the user
      // for a filename
    }

    Printer printer = new Printer(data);
    GC gc = new GC(printer);
    PBox.setParameters(gc, printer, printer.getDPI(), 100);
    if (printer.startJob("DoSys Druckauftrag")) {
      printer.startPage();
      doc.layout();
      doc.draw(1);
      printer.endJob();
    }
    gc.dispose();

  }

  public static void main(String[] args) {
    new PrintSWTTableExample();
  }
}
